"use strict";
var Module = {};
var initializedJS = false;

function threadPrintErr() {
  var text = Array.prototype.slice.call(arguments).join(" ");
  console.error(text)
}

function threadAlert() {
  var text = Array.prototype.slice.call(arguments).join(" ");
  postMessage({
    cmd: "alert",
    text: text,
    threadId: Module["_pthread_self"]()
  })
}
var err = threadPrintErr;
self.alert = threadAlert;
Module["instantiateWasm"] = (info, receiveInstance) => {
  var module = Module["wasmModule"];
  Module["wasmModule"] = null;
  var instance = new WebAssembly.Instance(module, info);
  return receiveInstance(instance)
}
self.onunhandledrejection = e => {
  throw e.reason ?? e
};

function handleMessage(e) {
  try {
    if (e.data.cmd === "load") {
      let messageQueue = [];
      self.onmessage = e => messageQueue.push(e);
      self.startWorker = instance => {
        Module = instance;
        postMessage({
          "cmd": "loaded"
        });
        for (let msg of messageQueue) {
          handleMessage(msg)
        }
        self.onmessage = handleMessage
      };
      Module["wasmModule"] = e.data.wasmModule;
      for (const handler of e.data.handlers) {
        Module[handler] = function () {
          postMessage({
            cmd: "callHandler",
            handler: handler,
            args: [...arguments]
          })
        }
      }
      Module["wasmMemory"] = e.data.wasmMemory;
      Module["buffer"] = Module["wasmMemory"].buffer;
      Module["ENVIRONMENT_IS_PTHREAD"] = true;
      (e.data.urlOrBlob ? import(e.data.urlOrBlob) : import("./ffmpeg-core.js")).then(exports =>
        exports.default(Module))
    } else if (e.data.cmd === "run") {
      Module["__emscripten_thread_init"](e.data.pthread_ptr, 0, 0, 1);
      Module["__emscripten_thread_mailbox_await"](e.data.pthread_ptr);
      Module["establishStackSpace"]();
      Module["PThread"].receiveObjectTransfer(e.data);
      Module["PThread"].threadInitTLS();
      if (!initializedJS) {
        initializedJS = true
      }
      try {
        Module["invokeEntryPoint"](e.data.start_routine, e.data.arg)
      } catch (ex) {
        if (ex != "unwind") {
          throw ex
        }
      }
    } else if (e.data.cmd === "cancel") {
      if (Module["_pthread_self"]()) {
        Module["__emscripten_thread_exit"](-1)
      }
    } else if (e.data.target === "setimmediate") {} else if (e.data.cmd === "checkMailbox") {
      if (initializedJS) {
        Module["checkMailbox"]()
      }
    } else if (e.data.cmd) {
      err("worker.js received unknown command " + e.data.cmd);
      err(e.data)
    }
  } catch (ex) {
    if (Module["__emscripten_thread_crashed"]) {
      Module["__emscripten_thread_crashed"]()
    }
    throw ex
  }
}
self.onmessage = handleMessage;
